package com.example.mallcart.controller;

import com.example.mallcart.controller.vo.CartItem;
import com.example.mallcart.entity.Cart;
import com.example.mallcart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * @author guanglin.ma
 * @date 2024-01-07 9:27
 */
@Controller
public class CartController {

    @Autowired
    CartService cartService;

    @RequestMapping("/add-cart")
    public String addCart(@RequestParam("skuId") String skuId, @RequestParam("num") String num, Model model) throws Exception {
        CartItem cartItem=cartService.addCart(skuId, num);
        model.addAttribute("item",cartItem);
        return "success";
    }

    @GetMapping("/cart-list")
    public String qureyCartList(Model model) throws IOException {
        Cart cart=cartService.getCartList();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    @GetMapping("/getUserCartItems")
    @ResponseBody
    List<CartItem> getUserCartItems() throws IOException {
        return cartService.getUserCartItems();
    }
}
